package cn.yqn.user.service.impl;

import cn.yqn.basic.constants.BaseConstants;
import cn.yqn.basic.exception.BusinessException;
import cn.yqn.basic.utils.MD5Utils;
import cn.yqn.basic.utils.StrUtils;
import cn.yqn.user.domain.Logininfo;
import cn.yqn.user.domain.User;
import cn.yqn.user.dto.RegisterDto;
import cn.yqn.user.mapper.LogininfoMapper;
import cn.yqn.user.mapper.UserMapper;
import cn.yqn.user.service.IUserService;
import cn.yqn.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yqn
 * @since 2022-07-28
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(RegisterDto registerDto) {
        String phone = registerDto.getPhone();
        String password = registerDto.getPassword();
        String verifyCode = registerDto.getVerifyCode();
        String comfirmPassword = registerDto.getComfirmPassword();
        // 1.效验前端参数是否为空
        if (StringUtils.isEmpty(phone) ||
            StringUtils.isEmpty(password) ||
            StringUtils.isEmpty(verifyCode) ||
            StringUtils.isEmpty(comfirmPassword)
        ) {
            throw new BusinessException("参数不能为空!");
        }
        // 2.效验密码是否相同
        if (!password.equals(comfirmPassword)) {
            throw new BusinessException("两次密码不同,请确认密码");
        }
        // 3.效验手机账号是否注册
        User byPhone = userMapper.findByPhone(phone);
        if (byPhone != null) {
            throw new BusinessException("此手机账号已注册,不可多次注册");
        }
        // 4.判断验证码是否过期
        Object obj = redisTemplate.opsForValue().get(phone + BaseConstants.VerifyCodeConstant.BUSINESS_LOGIN_KEY);
        if (obj == null) {
            throw new BusinessException("验证码超时,请重新获取");
        }
        // 5.效验验证码是否正确
        String redisCode = obj.toString().split(":")[0];
        if (!verifyCode.equalsIgnoreCase(redisCode)) {
            throw new BusinessException("验证码有误,请从新确认!");
            }
        // 6.分别将数据保存到两张表中
        // 先去获取logininfo对象和user对象
        Logininfo logininfo = dto2Logininfo(registerDto);
        // 通过logininfo对象获取user对象
        User user = logininfo2User(logininfo);

        // 先保存logininfo表，返回自增的主键
        logininfoMapper.save(logininfo);

        // 把自增的主键赋值给user对象， 再保存user。
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);

    }

    private User logininfo2User(Logininfo logininfo) {

        User user = new User();
        // copy相同的属性
        BeanUtils.copyProperties(logininfo,user);

        return  user;
    }

    /**
     * 创建logininfo对象
     * @param dto
     * @return
     */
    public Logininfo dto2Logininfo(RegisterDto dto){
        Logininfo logininfo = new Logininfo();

        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setType(1);
        // 生成盐值 32
        String salt = StrUtils.getComplexRandomString(32);
        // 通过和密码进行加密   加盐加密：
        String dbPassword = MD5Utils.encrypByMd5(salt + dto.getPassword());
        logininfo.setSalt(salt);
        logininfo.setPassword(dbPassword);

        return logininfo;
    }

}

